<?php


namespace App\Service;

use App\Common\Tools\DbTools;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\FlyGourmetMealClassify;
use App\Model\Product;
use Hyperf\Di\Annotation\Inject;
use http\Exception;

/**
 *
 * 商户美食配置
 * Class FlyGourmetMealService
 * @package App\Service
 */
class FlyGourmetMealService extends BaseService
{
    /**
     * 审核
     * @Inject
     * @var ProductService
     */
    private $productService;

    /**
     * @Inject
     * @var LinkFileService
     */
    private $LinkFileService;

    /**
     * @Inject
     * @var SkuService
     */
    private $SkuService;

    /**
     * Notes:
     * @param int $shopId
     * @param array $param
     * @param int $type
     * @return object
     * Date: 2022/7/21
     * Time:17:31
     * Auth: zdl
     */
    public function index(int $shopId,array $param=[],$type=1)
    {
        $push_by = isset($param['push_by']) ? $param['push_by'] :1;
        $order_by = isset($param['order_by']) ? intval($param['order_by']) : '1';
        $order_by_name = 'push_at';
        $order = 'ASC';
        switch ($order_by){
            case 1:
                $order_by_name = 'views_times';
                $order = 'DESC';
                break;
            case 2:
                $order_by_name = 'sales_actual';
                $order = 'DESC';
                break;
        }
        if($type==1){
            $is_illegal = 0;
            $check_status = 1;
        }else{
            $is_illegal = (isset($param['is_illegal'])&&$param['is_illegal']!=='')?$param['is_illegal']:'';
            $check_status = isset($param['check_status'])?$param['check_status'] : '';
        }
        $is_show = isset($param['is_show'])?$param['is_show'] : '';
        $key_word = $param['key_word']?:'';
        $where = [];
        $where['type'] = 2;
        $where['shop_id'] = $shopId;
        if($is_illegal!==''){
            $where['is_illegal']   =  $is_illegal;
        }
        if($is_show!==''){
            $where['is_show']   =  $is_show;
        }
        if ($check_status !== '') {
            $where['check_status'] = $check_status;
        }
        if ($check_status !== '') {
            $where['check_status'] = $check_status;
        }
        return  Product::query()
            ->where($where)
            ->when($key_word,function ($query)use($key_word){
                $query->where('product_name','like','%'.$key_word.'%');
            })
            ->orderBy('push_at',$push_by==1?'DESC':'ASC')
            ->orderBy($order_by_name,$order)
            ->with(['shop_info', 'cate_info', 'sku_list'])
            ->with(['reason_info', 'refuse_info', 'main_pic','file_info'])
            ->orderBy('push_at',$push_by==1?'DESC':'ASC')
            ->orderBy($order_by_name,$order);
    }

    /**
     * Notes:
     * @param int $shopId
     * @param array $param
     * @return int
     * Date: 2022/7/23
     * Time:10:27
     * Auth: zdl
     */
    public function add(int $shopId, array $param)
    {
        $sku = $main_pic = [];
        $param['min_single_price'] = 0;
        if(isset($param['sku'])){
            $sku = $param['sku']; unset($param['sku']);
            $price = array_column($sku, 'single_price');
            sort($price, 1);
            $param['min_single_price'] = $price['0'];
        }
        if(isset($param['main_pic'])){
            $main_pic = $param['main_pic']; unset($param['main_pic']);
        }
        $param['shop_id'] = $shopId;
        $param['type'] = 2;
        $product_id = $this->productService->insertGetId($param);
        $this->LinkFileService->link_file_insert($main_pic, $product_id, 2);
        $this->SkuService->sku_insert($sku,intval($product_id),1);
        return $product_id;
    }



    /**
     * Notes:
     * @param int $shopId
     * @param array $param
     * @return bool
     * Date: 2022/7/23
     * Time:10:28
     * Auth: zdl
     */
    public function update(int $shopId, array $param)
    {
        $sku = $main_pic = [];
        if(isset($param['sku'])){
            $sku = $param['sku']; unset($param['sku']);
            $price = array_column($sku, 'single_price');
            sort($price, 1);
            $param['min_single_price'] = $price['0'];
        }
//        通过productservice修改商品
        $res = $this->productService->product_update($param);
        $this->SkuService->sku_insert($sku,intval($param['id']),2);
        return $param['id'];
    }

    /**
     * Notes:美食详情
     * @param int $id
     * @param string $lng
     * @param string $lat
     * @return object|null
     * Date: 2022/7/25
     * Time:10:18
     * Auth: zdl
     */
    public function info(int $id,$lng = '', $lat = '')
    {
        $info =   Product::query()
            ->where('type',2)
            ->where('id',$id)
            ->with(['shop_info', 'cate_info', 'sku_list','reviews_list'])
            ->withCount('reviews')
            ->with(['reason_info', 'refuse_info', 'main_pic','file_info'])->first();
        if ($lat && $lng) {
            $info->distance = DbTools::getPointDistance(floatval($lng), floatval($lat), floatval($info->shop_info->lng), floatval($info->shop_info->lat));
        } else {
            $info->distance = 0;
        }
        return $info;
    }

    /**
     * 统计
     * @param int $shop_id
     * @param int $type
     * @return object
     */
    public function countSize($shop_id,$type=2)
    {
        return  Product::query()
            ->where('shop_id',$shop_id)
            ->where('type',$type)
            ->selectRaw("
                count(case when is_show !=3 then 1 end) as total_num,
                count(case when (is_show = 1 AND check_status=1 AND is_illegal=0) then 1 end) as show_num,
                count(case when (is_show = 0 AND check_status=1 AND is_illegal=0) then 1 end) as un_show_num,
                count(case when is_illegal=1 then 1 end) as illegal_num",
                )
            ->first();
    }
}